package com.ruoyi.mobile.project.oms.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.db.mobile.mapper.WxOrderMapper;
import com.ruoyi.db.oms.domain.OmsOrder;
import com.ruoyi.db.oms.domain.query.OmsOrderQueryVo;
import com.ruoyi.db.oms.mapper.OmsOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class WxOmsOrderService
{
    @Autowired
    private OmsOrderMapper orderMapper;

    @Autowired
    private WxOrderMapper wxOrderMapper;

    /**
     * 添加订单
     *
     * @author 严方亚
     * @date 2020-05-22
     * @param order
     */
    public int add(OmsOrder order)
    {
        order.setAddTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        return orderMapper.insertOmsOrder(order);
    }

    public int count(Long userId)
    {
        return 0;
    }

    public OmsOrder findById(Long orderId)
    {
        return null;
    }

    /**
     * 订单列表查询对象 oms_order
     *
     * @author 严方亚
     * @date 2020-05-12
     * @param userId
     * @param orderId
     */
    public OmsOrder findById(Long userId, Long orderId)
    {
        OmsOrderQueryVo orderQueryVo = new OmsOrderQueryVo();
        orderQueryVo.setUserId(userId);
        orderQueryVo.setId(orderId);
        return orderMapper.selectOmsOrderByUserIdAId(orderQueryVo);
    }

    private String getRandomNum(Integer num)
    {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++)
        {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public int countByOrderSn(Long userId, String orderSn)
    {
        return 0;
    }

    // TODO 这里应该产生一个唯一的订单，但是实际上这里仍然存在两个订单相同的可能性
    public String generateOrderSn(Long userId)
    {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        String now = df.format(LocalDate.now());
        String orderSn = now + getRandomNum(6);
        while (countByOrderSn(userId, orderSn) != 0)
        {
            orderSn = now + getRandomNum(6);
        }
        return orderSn;
    }

    /**
     * 订单列表查询对象 oms_order
     *
     * @author 严方亚
     * @date 2020-05-12
     */
    public List<OmsOrder> queryByOrderStatus(Long userId, Integer orderStatus, Integer page, Integer limit, String sort, String order)
    {
        // 设置查询参数值
        OmsOrderQueryVo orderQueryVo = new OmsOrderQueryVo();
        orderQueryVo.setUserId(userId);
        if(orderStatus != null){
            orderQueryVo.setOrderStatus(orderStatus);
        }
        // 排序
        orderQueryVo.setOrder(order);
        orderQueryVo.setSort(sort);
        // 分页
        PageHelper.startPage(page, limit);

        return orderMapper.selectOmsOrderList(orderQueryVo);
    }

    public List<OmsOrder> querySelective(Long userId, String orderSn, LocalDateTime start, LocalDateTime end, List<Short> orderStatusArray, Integer page, Integer limit, String sort, String order)
    {
        return null;
    }

    /**
     * 修改订单状态
     *
     * @author 严方亚
     * @date 2020-05-13
     * @param order
     */
    public int updateWithOptimisticLocker(OmsOrder order)
    {
        LocalDateTime lastUpdateTime = order.getUpdateTime();
        order.setUpdateTime(LocalDateTime.now());
        return wxOrderMapper.updateWithOptimisticLocker(lastUpdateTime, order);
    }

    /**
     * 删除订单，逻辑删除
     *
     * @author 严方亚
     * @date 2020-05-13
     * @param id
     */
    public void deleteById(Long id)
    {
        orderMapper.deleteOmsOrderById(id);
    }

    public int count()
    {
        return 0;
    }

    public List<OmsOrder> queryUnpaid(int minutes)
    {
        return null;
    }

    public List<OmsOrder> queryUnconfirm(int days)
    {
        return null;
    }

    public OmsOrder findBySn(String orderSn)
    {
        return null;
    }

    public Map<Object, Object> orderInfo(Long userId)
    {
        return null;
    }

    public List<OmsOrder> queryComment(int days)
    {
        return null;
    }

    /**
     * 申请售后之后同步更新订单表的售后状态
     *
     * @author 严方亚
     * @date 2020-05-14
     * @param orderId
     * @param statusReject
     */
    public void updateAftersaleStatus(Long orderId, Integer statusReject)
    {
        OmsOrder order = new OmsOrder();
        order.setId(orderId);
        order.setAftersaleStatus(statusReject);
        order.setUpdateTime(LocalDateTime.now());
        orderMapper.updateOmsOrder(order);
    }
}
